﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GameNode
{
    class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        private Random rnd;

        public Node(int x, int y)
        {
            X = x;
            Y = y;
            rnd = new Random();
            //Thread.Sleep(100);
        }

        public void MoveUp()
        { Y--; }

        public void MoveDown()
        { Y++; }

        public void MoveLeft()
        { X--; }

        public void MoveRight()
        { X++; }

        public bool IsOnPossition(int positionX, int positionY)
        {
            if(X == positionX && Y == positionY)
                return true;
            return false;
        }

        public virtual void Move(Func<int,int,Position> positionStatus)
        {
            int randomMove = rnd.Next(1, 5);
            switch (randomMove)
            {
                case 1:
                    {
                        //MoveUp
                        if (positionStatus(X,Y-1) == Position.Free)
                            //IsPositionFree(node.X, node.Y - 1) && gameField.IsOnField(node.X, node.Y - 1))
                            MoveUp();
                    }
                    break;

                case 2:
                    {
                        //MoveDown
                        if (positionStatus(X, Y + 1) == Position.Free)
                            //IsPositionFree(node.X, node.Y + 1) &&
                            //gameField.IsOnField(node.X, node.Y + 1))
                            MoveDown();
                    }
                    break;

                case 3:
                    {
                        //MoveLeft
                        if (positionStatus(X-1, Y) == Position.Free)
                            //IsPositionFree(node.X - 1, node.Y) &&
                            //gameField.IsOnField(node.X - 1, node.Y))
                            MoveLeft();
                    }
                    break;

                case 4:
                    {
                        //MoveRight
                        if (positionStatus(X+1, Y) == Position.Free)
                            //IsPositionFree(node.X + 1, node.Y) &&
                            //gameField.IsOnField(node.X + 1, node.Y))
                            MoveRight();
                    }
                    break;
            }
        }
    }

    class ConsoleNode : Node
    {
        public char NodeSymbol { get; private set; }
        public ConsoleColor NodeColor { get; private set; }

        public ConsoleNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) 
            : base(x,y)
        {
            NodeSymbol = nodeSymbol;
            NodeColor = nodeColor;
        }

    }

    class MaksimGameNode : ConsoleNode
    {
        enum MoveDirection
        {
            Up,
            Down
        }

        private bool isOnTop;
        private bool isOnLeft;
        private MoveDirection direction;

        public MaksimGameNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) : base(x, y, nodeSymbol, nodeColor)
        {
            direction = MoveDirection.Down;
        }
        public override void Move(Func<int, int, Position> positionStatus)
        {
            if (!isOnTop)
            {
                if (positionStatus(X, Y - 1) == Position.Free)
                {
                    MoveUp();
                }
                else if (positionStatus(X, Y - 1) == Position.Border)
                    isOnTop = true;
            }
            else if (!isOnLeft)
            {
                if (positionStatus(X - 1, Y) == Position.Free)
                {
                    MoveLeft();
                }
                else if (positionStatus(X - 1, Y) == Position.Border)
                    isOnLeft = true;
            }
            else if (isOnTop && isOnLeft)
            {
                if (direction == MoveDirection.Down)
                {
                    if (positionStatus(X, Y + 1) == Position.Free)
                    {
                        MoveDown();
                    }
                    else if (positionStatus(X, Y + 1) == Position.Border)
                    {
                        if (positionStatus(X + 1, Y) == Position.Free)
                        {
                            MoveRight();
                            direction = MoveDirection.Up;
                        }
                    }
                }
                else if (direction == MoveDirection.Up)
                {
                    if (positionStatus(X, Y - 1) == Position.Free)
                    {
                        MoveUp();
                    }
                    else if (positionStatus(X, Y - 1) == Position.Border)
                    {
                        if (positionStatus(X + 1, Y) == Position.Free)
                        {
                            MoveRight();
                            direction = MoveDirection.Down;
                        }
                    }
                }

            }
        }

    }
}
